﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class customLensIndexManager : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            CSUtils.CheckSession();
            this.binddata();            
            PanelInput.Visible = false;
        }
        

    }

    private void binddata() {
        GridView1.DataSource = this.getAllLensIndex();
        GridView1.DataBind();
    }


    private DataSet getAllLensIndex()
    {
        string sql =
            "select * " +
            "from cs_custom_lens_index " +
            "order by index_code desc, index_name";

        DataSet ds = new DataSet();
        DBConn myDB = new DBConn();
        ds = myDB.getDataSet(sql);
        myDB.Close();
        return ds;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        GridView1.PageIndex = e.NewPageIndex;
        this.binddata();
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {        

        if (e.CommandName.Equals("command_edit") ||
            e.CommandName.Equals("command_add")) {
            PanelInput.Visible = true;
            TextBoxCommandType.Text = e.CommandName;
            LabelIndexID.Text = e.CommandArgument.ToString();
            BaseVO vo = CSUtils.getLensIndex(LabelIndexID.Text);
            TextBoxIndexName.Text = vo.Name;
            DropDownListIndexCode.SelectedValue = vo.Code;
        }
        else {
            this.cleanPanelInput();
        }        
        
    }


    protected void ButtonSubmit_Click(object sender, EventArgs e) {
        
        string indexID = LabelIndexID.Text;
        string indexName = TextBoxIndexName.Text;
        string indexCode = DropDownListIndexCode.SelectedValue;

        string commandType = TextBoxCommandType.Text;
        BaseVO vo = null;
        if (commandType.Equals("command_edit"))
        {
            //get vo from db
            vo = CSUtils.getLensIndex(indexID);

            //update there info
            vo.Name = indexName;
            vo.Code = indexCode;            
        }
        else if (commandType.Equals("command_add"))
        {
            vo = new BaseVO();
            vo.Name = indexName;
            vo.Code = indexCode;  
        }

        if (CSUtils.mergeLensIndex(vo))
        {
            //JScript.Alert("保存成功！");
        }
        else
        {
            JScript.Alert("保存失败！");
        }

        this.binddata();
        
    }

    protected void LinkButtonAdd_Click(object sender, EventArgs e) {

        PanelInput.Visible = true;
        TextBoxCommandType.Text = "command_add";
        this.cleanPanelInput();

    }



    private void cleanPanelInput() {

        LabelIndexID.Text = "";
        TextBoxIndexName.Text = "";
        DropDownListIndexCode.SelectedIndex = 0;

    }
}
